package com.umeng.soexample;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.dankegongyu.lib.share.DKShareButton;
import com.dankegongyu.lib.share.DKShareCallback;
import com.dankegongyu.lib.share.DKShareInterceptor;
import com.dankegongyu.lib.share.DKShareOauthUtil;
import com.dankegongyu.lib.share.DKShareOnOauthListener;
import com.dankegongyu.lib.share.DKShareUtil;
import com.dankegongyu.lib.share.DK_SHARE_MEDIA;
import com.dankegongyu.lib.share.bean.DKShareImage;
import com.dankegongyu.lib.share.bean.DKShareMiniProgram;
import com.dankegongyu.lib.share.bean.DKShareText;
import com.dankegongyu.lib.share.bean.DKShareWeb;

import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = MainActivity.class.getSimpleName();
    private final DK_SHARE_MEDIA[] MEDIAS = {DK_SHARE_MEDIA.WEIXIN, DK_SHARE_MEDIA.WEIXIN_CIRCLE, DK_SHARE_MEDIA.SINA};

    private ListView mListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String[] mPermissionList = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.CALL_PHONE, Manifest.permission.READ_LOGS, Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.SET_DEBUG_APP, Manifest.permission.SYSTEM_ALERT_WINDOW, Manifest.permission.GET_ACCOUNTS, Manifest.permission.WRITE_APN_SETTINGS};
        ActivityCompat.requestPermissions(this, mPermissionList, 123);
        mListView = findViewById(R.id.listView);
        mListView.setOnItemClickListener(mOnItemClickListener);
        mListView.setAdapter(new ShareAdapter(this, Arrays.asList(getResources().getStringArray(R.array.shareItems))));
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    private AdapterView.OnItemClickListener mOnItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//            Toast.makeText(MainActivity.this, "share " + i, Toast.LENGTH_SHORT).show();
            switch (i) {
                case 0: // 分享小程序，无标题栏
                    shareWeb();
                    break;
                case 1: // 分享链接，有标题栏，自定义显示顺序
                    shareWebWithSinaAt();
                    break;
                case 2: // 分享图片，无标题栏，不显示朋友圈
                    shareImage();
                    break;
                case 3: // 分享图片，有标题栏
                    shareImageWithSinaAt();
                    break;
                case 4: // 自定义分享
                    ShareDialog.launch(MainActivity.this);
                    break;
                case 5: // 直接分享链接，不弹框
                    shareWebDirectly();
                    break;
                case 6: // 直接分享图片，不弹框
                    shareImageDirectly();
                    break;
                case 7: // 只分享文字
                    shareText();
                    break;
                case 8: // 微信授权
                    weixinOauth();
                    break;
            }
        }
    };

    private void shareWeb() {
        DKShareMiniProgram miniProgram = new DKShareMiniProgram.Builder()
                .setShareUrl(Constants.SHARE_URL)
                .setShareTitle(Constants.SHARE_TITLE)
                .setShareContent(Constants.SHARE_CONTENT)
                .setShareImageUrl(Constants.SHARE_THUMB)
                .setPath("pages/page10007/page10007")
                .setUserName("gh_3ac2059ac66f")
                .build();

        DKShareWeb.with(this)
                .setCustomPlatforms(new DKShareButton(Constants.PLATFORM_MORE, "更多", R.mipmap.ic_launcher_round, 3))
                .setShareUrl(Constants.SHARE_URL)
                .setShareTitle(Constants.SHARE_TITLE)
                .setShareImageUrl(Constants.SHARE_THUMB)
                .setMiniProgram(miniProgram)
                .setInterceptor(new DKShareInterceptor<DKShareWeb>() {
                    @Override
                    public void interceptor(DKShareWeb dkShareWeb, DK_SHARE_MEDIA share_media) {
//                        if (DK_SHARE_MEDIA.SINA.equals(share_media)) {
//                            dkShareWeb.setShareContent(Constants.SHARE_CONTENT + Constants.SINA_PREFIX);
//                        }
                    }
                })
                .show(new DKShareCallback() {
                    @Override
                    public void onStart(String platform) {
                    }

                    @Override
                    public void onResult(String platform, boolean success) {
                        if (Constants.PLATFORM_MORE.equals(platform)) {
                            Toast.makeText(MainActivity.this, "自定义操作", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    private void shareWebWithSinaAt() {
        DKShareWeb.with(this)
                .setCustomPlatforms(new DKShareButton(Constants.PLATFORM_MORE, "更多", R.mipmap.ic_launcher_round, 1))
                .setTitle("分享友盟")
                .setSubTitle("说一下你为什么愿意分享友盟~")
                .setShareUrl(Constants.SHARE_URL)
                .setShareTitle(Constants.SHARE_TITLE)
                .setShareImageUrl(Constants.SHARE_THUMB)
                .setShareImageIcon(R.drawable.ic_share)
                .setInterceptor(new DKShareInterceptor<DKShareWeb>() {
                    @Override
                    public void interceptor(DKShareWeb dkShareWeb, DK_SHARE_MEDIA share_media) {
//                        if (DK_SHARE_MEDIA.SINA.equals(share_media)) {
//                            dkShareWeb.setShareContent(Constants.SHARE_CONTENT + Constants.SINA_PREFIX);
//                        }
                    }
                })
                .show(new DKShareCallback() {
                    @Override
                    public void onStart(String platform) {
                        Log.e(TAG, "分享开始 " + platform);
                    }

                    @Override
                    public void onResult(String platform, boolean success) {
                        Log.e(TAG, "分享成功 " + platform);
                    }
                });
    }

    private void shareImage() {
        DKShareImage.with(this)
                .setPlatforms(DK_SHARE_MEDIA.WEIXIN)
                .setCustomPlatforms(new DKShareButton(Constants.PLATFORM_MORE, "更多", R.mipmap.ic_launcher_round, 0))
                .setImageUrl(Constants.SHARE_IMAGE)
                .setShareTitle(Constants.SHARE_TITLE)
                .setShareContent(Constants.SHARE_CONTENT)
                .setInterceptor(new DKShareInterceptor<DKShareImage>() {
                    @Override
                    public void interceptor(DKShareImage dkShareImage, DK_SHARE_MEDIA share_media) {
//                        if (DK_SHARE_MEDIA.SINA.equals(share_media)) {
//                            dkShareImage.setShareContent(Constants.SHARE_CONTENT + Constants.SINA_PREFIX);
//                        }
                    }
                })
                .show();
    }

    private void shareImageWithSinaAt() {
        DKShareImage.with(this)
                .setPlatforms(DK_SHARE_MEDIA.SINA)
                .setTitle("分享友盟")
                .setSubTitle("你用友盟吗？觉得好玩吗？")
                .setImageUrl(Constants.SHARE_IMAGE)
                .setShareTitle(Constants.SHARE_TITLE)
                .setShareContent(Constants.SHARE_CONTENT)
                .setInterceptor(new DKShareInterceptor<DKShareImage>() {
                    @Override
                    public void interceptor(DKShareImage dkShareImage, DK_SHARE_MEDIA share_media) {
//                        if (DK_SHARE_MEDIA.SINA.equals(share_media)) {
//                            dkShareImage.setShareContent(Constants.SHARE_CONTENT + Constants.SINA_PREFIX);
//                        }
                    }
                })
                .show(new DKShareCallback() {
                    @Override
                    public void onStart(String platform) {
                        Log.e(TAG, "分享开始 " + platform);
                    }

                    @Override
                    public void onResult(String platform, boolean success) {
                        Log.e(TAG, "分享成功 " + platform);
                    }
                });
    }

    private AtomicInteger webIndex = new AtomicInteger(0);

    private void shareWebDirectly() {
        if (webIndex.get() == MEDIAS.length) {
            webIndex.set(0);
        }
        DKShareWeb.with(this)
                .setShareUrl(Constants.SHARE_URL)
                .setShareTitle(Constants.SHARE_TITLE)
                .setShareImageUrl(Constants.SHARE_THUMB)
                .setShareImageIcon(R.drawable.ic_share)
                .shareDirect(MEDIAS[webIndex.getAndIncrement()], new DKShareCallback() {
                    @Override
                    public void onStart(String platform) {
                        Log.e(TAG, "分享开始 " + platform);
                    }

                    @Override
                    public void onResult(String platform, boolean success) {
                        Log.e(TAG, platform + "分享是否成功：" + success);
                    }
                });
        Log.e(TAG, "直接分享链接" + webIndex.get());
    }

    private AtomicInteger imageIndex = new AtomicInteger(0);

    private void shareImageDirectly() {
        if (imageIndex.get() == MEDIAS.length) {
            imageIndex.set(0);
        }
        DKShareImage.with(this)
                .setImageUrl(Constants.SHARE_IMAGE)
                .setShareTitle(Constants.SHARE_TITLE)
                .setShareContent(Constants.SHARE_CONTENT)
                .shareDirect(MEDIAS[imageIndex.getAndIncrement()], new DKShareCallback() {
                    @Override
                    public void onStart(String platform) {
                        Log.e(TAG, "分享开始 " + platform);
                    }

                    @Override
                    public void onResult(String platform, boolean success) {
                        Log.e(TAG, platform + "分享是否成功：" + success);
                    }
                });
        Log.e(TAG, "直接分享图片" + imageIndex.get());
    }

    private void shareText() {
        DKShareText.with(this)
                .setShareContent("只分享文字蛋壳公寓让租房变得简单和快乐")
                .show(new DKShareCallback() {
                    @Override
                    public void onStart(String platform) {
                        Log.e(TAG, "分享开始 " + platform);
                    }

                    @Override
                    public void onResult(String platform, boolean success) {
                        Log.e(TAG, platform + "分享是否成功：" + success);
                    }
                });
    }

    private void weixinOauth() {
        DKShareOauthUtil.weixinOauth(this, new DKShareOnOauthListener() {
            @Override
            public void onStart(DK_SHARE_MEDIA dk_share_media) {
                Toast.makeText(MainActivity.this, "授权开始：" + dk_share_media, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onComplete(DK_SHARE_MEDIA dk_share_media, int status, Map<String, String> map) {
                Toast.makeText(MainActivity.this, "授权完成：" + dk_share_media, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(DK_SHARE_MEDIA dk_share_media, int status, Throwable throwable) {
                Toast.makeText(MainActivity.this, "授权失败：" + dk_share_media, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancel(DK_SHARE_MEDIA dk_share_media, int status) {
                Toast.makeText(MainActivity.this, "取消授权：" + dk_share_media, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // QQ与新浪不需要添加Activity，但需要在使用QQ分享或者授权的Activity中添加下面的代码
        // 注意onActivityResult不可在fragment中实现，
        // 如果在fragment中调用登录或分享，需要在fragment依赖的Activity中实现
        // https://developer.umeng.com/docs/66632/detail/66639
//        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
        DKShareUtil.onActivityResult(this, requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        DKShareUtil.release(this);
    }
}
